﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using zijian666.Converts;

namespace UnitTest
{
    class DiagnosticObserver : IObserver<DiagnosticListener>
    {
        public void OnCompleted()
        {

        }

        public void OnError(Exception error)
        {

        }

        public void OnNext(DiagnosticListener value)
        {
            if (!value.IsEnabled())
            {
                value.Subscribe(_tracing);
            }
        }

        static readonly Tracing _tracing = new Tracing();

        class Tracing : IObserver<KeyValuePair<string, object>>
        {
            public void OnCompleted()
            {

            }

            public void OnError(Exception error)
            {
                Console.WriteLine($"error: {error}");
            }

            public void OnNext(KeyValuePair<string, object> value)
            {
                if (value.Value is null)
                {
                    Console.WriteLine($"DiagnosticListener: {value.Key}, `null` (null)");
                }
                else
                {
                    Console.WriteLine($"DiagnosticListener: {value.Key}, `{value.Value}` ({value.Value.GetType().To<string>()})");
                }
            }
        }
    }
}
